package com.hgk.wanwan.main.fragment.whatfragment;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.common.android.volley.VolleyError;
import com.common.dongqiuhui.apps.comm.constants.UrlConstans;
import com.common.playing.apps.comm.tools.PlayingUtil;
import com.hgk.common.common.db.BaseDao;
import com.hgk.common.common.ui.fragment.SimpleBaseFragment;
import com.hgk.common.common.ui.view.BallGridView;
import com.hgk.common.common.ui.view.BannerGuidGallery;
import com.hgk.wanwan.chatactivity.ChatActivity2;
import com.hgk.wanwan.main.fragment.whatfragment.adapter.BannerAdapter;
import com.hgk.wanwan.main.fragment.whatfragment.adapter.PagerAdapter1;
import com.hgk.wanwan.main.fragment.whatfragment.adapter.PagerCenterAdapter;
import com.hgk.wanwan.main.fragment.whatfragment.adapter.WhatGridAdapter;
import com.hgk.wanwan.main.fragment.whatfragment.model.BannerBean;
import com.hgk.wanwan.main.msgmodel.WhatsBean;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import width.hgk.wanwan.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class WhatNowFragment extends SimpleBaseFragment {
    private final String GET_WHAT_WHOLE_SITUATION_URL = UrlConstans.ZWCOMMOMURL+"进入app此页面访问接口";
    private final String GET_FOLLOW_URL = UrlConstans.ZWCOMMOMURL+"/domain/user/follow_suggest.json";//推荐关注接口

    private BannerGuidGallery mBannerGuidGallery;
    private LinearLayout pointLinear;//上边的下标
    private List<BannerBean> bannerLists;
    private ImageView[] mDots;
    private BannerAdapter mBannerAdapter;

    private ViewPager mCenterPager,mBottomPager;//中间没有轮播效果
    private ImageView[] tips;//下标点
    private ImageView[] mImageViews;//图片
    private LinearLayout mPointCenter,mPointBottom;

    private String mType = "1";//搜索type
    //viewpager使用的参数
    private PagerAdapter1 pagerAdapter;

    private TextView mTvLife,mHouse,mTvArt;
    private ProgressDialog myDialog = null;

    //达人推荐使用参数
    private BaseDao mBaseDao;
    private Map<String,WhatsBean.Data> mStringDataMap = null;
    private BallGridView mGridView;
    private WhatGridAdapter mGridAdapter;
    private WhatsBean.Data datatas;

    //示例图片数组
    private List<Integer> lists;
    //图片资源
    private int[] pics ={R.drawable.pic_one,R.drawable.pic_five,R.drawable.pic_four,
            R.drawable.pic_six,R.drawable.pic_three,R.drawable.pic_two};

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        mBaseDao=new BaseDao(mContext);
        mStringDataMap = new TreeMap<String,WhatsBean.Data>(new Comparator<String>() {
            public int compare(String obj1, String obj2) {
                return obj2.compareTo(obj1);
            }
        });
    }

    //最多出现5个标签，最多两行，有不同的评价显示个数不同，、和不同的颜色
    private void initView(View view) {
        initHeaderViewPager(view);
        initCenterPager(view);
        initBottom(view);

        mHouse = (TextView) view.findViewById(R.id.tv_what_frag_house_two);
        mTvLife = (TextView) view.findViewById(R.id.tv_what_gridview_life);
        mTvArt = (TextView) view.findViewById(R.id.tv_what_gridview_art);
        mGridView = (BallGridView) view.findViewById(R.id.vp_what_fragment_grid_hot);
        mGridAdapter = new WhatGridAdapter(mContext);
        mGridView.setAdapter(mGridAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //TODO 点击GridView子项的事件
            }
        });
        mTvLife.setOnClickListener(this);
        mHouse.setOnClickListener(this);
        mTvArt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus==true){
                    mTvArt.setCompoundDrawables(null,getResources().getDrawable(R.drawable.icon_live_show),null,null);
                }else{
                    mTvArt.setCompoundDrawables(null,getResources().getDrawable(R.drawable.icon_frend),null,null);
                }
            }
        });

    }

    private void initHeaderViewPager(View view) {
        lists = new ArrayList<>();
        for (int pic:pics) {
            lists.add(pic);
        }
        mBannerGuidGallery = (BannerGuidGallery) view.findViewById(R.id.gr_jingxuang_header);
        pointLinear = (LinearLayout) view.findViewById(R.id.gallery_point_linear_jingxuan);
        RelativeLayout.LayoutParams igLp = (RelativeLayout.LayoutParams) mBannerGuidGallery.getLayoutParams();
        igLp.width = PlayingUtil.getScreenWidth(mContext);
        igLp.height = igLp.width / 7 * 3;
        initGallery(lists.size());
        mBannerGuidGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectPage2(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void initGallery(int size){
        mDots = new ImageView[size];
        pointLinear.removeAllViews();
        for (int i=0;i<size;i++){
            ImageView pointView = new ImageView(mContext);
            mDots[i] = pointView;
            pointLinear .addView(pointView);
            if (i==0){
                //默认进入app时，第一pic备选中
                mDots[i].setBackgroundResource(R.drawable.feature_point_cur);
            }else{
                mDots[i].setBackgroundResource(R.drawable.feature_point);
            }
            mBannerAdapter = new BannerAdapter(lists,mContext);
            mBannerGuidGallery.setAdapter(mBannerAdapter);
            mBannerGuidGallery.setVisibility(View.VISIBLE);
            mBannerGuidGallery.startScrollIma();
            mBannerGuidGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                    ImagePreviewActivity.launch(mContext,);
                    Toast.makeText(mContext, "第" + (position % lists.size() + 1) + "张图片被点击了", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private void initCenterPager(View view) {
        int type = 1;
        mCenterPager = (ViewPager) view.findViewById(R.id.vp_what_fragment_center);
        mPointCenter = (LinearLayout) view.findViewById(R.id.viewGroup);
        addPointView(type);//添加下表小点
        addImageView();
        //设置adapter
        mCenterPager.setAdapter(new PagerCenterAdapter(mContext, mImageViews));
        mCenterPager.setCurrentItem((mImageViews.length) * 100);
        mCenterPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setImageBackground(position % mImageViews.length);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initBottom(View view) {
        int type = 2;
        mBottomPager = (ViewPager) view.findViewById(R.id.vp_what_fragment_activity);
        mPointBottom = (LinearLayout) view.findViewById(R.id.viewGroup_down);
        addPointView(type);//添加下表小点
        addImageView();
        //设置adapter
        mBottomPager.setAdapter(new PagerCenterAdapter(mContext, mImageViews));
        mBottomPager.setCurrentItem((mImageViews.length) * 100);
        mBottomPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                setImageBackground(position % mImageViews.length);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void addImageView() {
        mImageViews = new ImageView[pics.length];
        for (int k = 0; k<mImageViews.length;k++){
            ImageView imageView2 = new ImageView(mContext);
            mImageViews[k] = imageView2;
            imageView2.setBackgroundResource(pics[k]);
        }
    }

    private void addPointView(int type) {
        tips = new ImageView[pics.length];//将下标点添加到ViewGroup中
        for (int j = 0;j<tips.length;j++){
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
            tips[j] = imageView;
            //设定下标点的颜色
            if (j==0){
                tips[j].setBackgroundResource(R.drawable.feature_point_cur);
            }else{
                tips[j].setBackgroundResource(R.drawable.feature_point);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin =0;
            layoutParams.rightMargin =0;
            if (type==1) {
                mPointCenter.addView(imageView, layoutParams);
            }else if(type==2){
                mPointBottom.addView(imageView,layoutParams);
            }
        }
    }

    /**
     * 设置选中的tip的背景
     * @param selectItems
     */
    private void setImageBackground(int selectItems){
        for(int i=0; i<tips.length; i++){
            if(i == selectItems){
                tips[i].setBackgroundResource(R.drawable.feature_point_cur);
            }else{
                tips[i].setBackgroundResource(R.drawable.feature_point);
            }
        }
    }

    private void selectPage(int size) {
//        /** 设置当前显示的页码 */
//        if (size <bannerLists.size() ) {
//        } else {
//            size = size % bannerLists.size();
//
//        }
//        for (int i = 0; i < bannerLists.size(); i++) {
//            if (i == size) {
//                mDots[i].setBackgroundResource(R.drawable.feature_point_cur);
//            } else {
//                mDots[i].setBackgroundResource(R.drawable.feature_point);
//            }
//        }
    }
private void selectPage2(int size) {
    /** 设置当前显示的页码 */
    if (size <lists.size() ) {
    } else {
        size = size % lists.size();

    }
    for (int i = 0; i < lists.size(); i++) {
        if (i == size) {
            mDots[i].setBackgroundResource(R.drawable.feature_point_cur);
        } else {
            mDots[i].setBackgroundResource(R.drawable.feature_point);
        }
    }
}

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_what_gridview_life:
                //实现聊天界面demo
                ChatActivity2.launch(mContext);
                break;
            case R.id.tv_what_frag_house_two:
                final CharSequence strDialogTitle = getString(R.string.dialogtitle);
                final CharSequence strDialogMsg = getString(R.string.dialogmessage);
                myDialog = ProgressDialog.show(mContext,strDialogTitle,strDialogMsg,true);
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }finally {
                            myDialog.dismiss();
                        }
                    }
                }.start();
                break;
        }
    }

    @Override
    protected Map getApiParams() {
        return null;
    }

    @Override
    protected void success(String action, Object result) {

    }

    @Override
    protected void fail(String action, String errorStr) {

    }

    @Override
    protected void netfail(String action, VolleyError volleyError) {

    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setTitleView(LinearLayout linearlayout) {
//        linearlayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.what_fragment_title,null);
//        linearlayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //TODO 跳转到搜索页面进行搜索
//                SearchActivity.launch(mContext,mType);
//            }
//        });
    }

    @Override
    public Object getTitleString() {
        return "玩啥";
    }

    @Override
    public int getBodyView() {
        return R.layout.fragment_what_now;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBannerGuidGallery.stopScrolImg();
    }

}
